package com.adtec.base.cloud.baseservice.rbac.system.controller;


import com.adtec.base.cloud.baseservice.rbac.system.model.RoleCheckedIds;
import com.adtec.base.cloud.baseservice.rbac.system.model.SysMenuNode;
import com.adtec.base.cloud.baseservice.rbac.system.service.SysMenuService;
import com.adtec.base.cloud.commons.exception.AjaxResponse;
import com.adtec.base.cloud.persistence.system.model.SysMenu;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysmenu")
@Tag(name = "sysmenu", description = "菜单管理")
public class SysMenuController {

    @Resource
    private SysMenuService sysmenuService;

    //菜单管理：查询
    @PostMapping(value = "/tree")
    @Operation(description = "菜单管理：查询")
    @Parameters({
            @Parameter(name = "menuNameLike", description = "菜单名称(模糊查询)"),
            @Parameter(name = "menuStatus", description = "菜单状态"),
    })
    public List<SysMenuNode> tree(@RequestParam(name = "menuNameLike", required = false) String menuNameLike,
                                  @RequestParam(name = "menuStatus", required = false) Boolean menuStatus) {

        return sysmenuService.getMenuTree(menuNameLike, menuStatus);
    }

    //菜单管理：修改
    @PostMapping(value = "/update")
    @Operation(description = "菜单管理：修改")
    public AjaxResponse update(@RequestBody SysMenu sysMenu) {
        sysmenuService.updateMenu(sysMenu);
        return AjaxResponse.success("更新菜单项成功！");
    }

    //菜单管理：新增
    @PostMapping(value = "/add")
    @Operation(description = "菜单管理：新增")
    public AjaxResponse add(@RequestBody SysMenu sysMenu) {
        sysmenuService.addMenu(sysMenu);
        return AjaxResponse.success("新增菜单项成功！");
    }

    //菜单管理：删除
    @PostMapping(value = "/delete")
    @Operation(description = "菜单管理：删除")
    public AjaxResponse delete(@RequestBody SysMenu sysMenu) {
        sysmenuService.deleteMenu(sysMenu);
        return AjaxResponse.success("删除菜单项成功!");
    }

    //角色管理:菜单树展示（勾选项、展开项）
    @PostMapping(value = "/checkedtree")
    @Operation(description = "角色管理:菜单树展示（勾选项、展开项）")
    @Parameters({
            @Parameter(name = "roleId", description = "角色ID"),
    })
    public Map<String, Object> checkedtree(@RequestParam Long roleId) {
        Map<String, Object> ret = new HashMap<>();
        ret.put("tree", sysmenuService.getMenuTree("", null));
        ret.put("expandedKeys", sysmenuService.getExpandedKeys());
        ret.put("checkedKeys", sysmenuService.getCheckedKeys(roleId));
        return ret;
    }

    //角色管理：保存菜单勾选结果
    @PostMapping(value = "/savekeys")
    @Operation(description = "角色管理：保存菜单勾选结果")
    public AjaxResponse savekeys(@RequestBody RoleCheckedIds roleCheckedIds) {
        sysmenuService.saveCheckedKeys(roleCheckedIds.getRoleId(), roleCheckedIds.getCheckedIds());
        return AjaxResponse.success("保存菜单权限成功!");
    }

    //系统左侧菜单栏加载，根据登录用户名加载它可以访问的菜单项
    @PostMapping(value = "/tree/user")
    @Operation(description = "系统左侧菜单栏加载，根据登录用户名加载它可以访问的菜单项")
    @Parameters({
            @Parameter(name = "username", description = "登陆用户名"),
    })
    public List<SysMenuNode> usertree(@RequestParam("username") String username) {
        return sysmenuService.getMenuTreeByUsername(username);
    }


    //菜单管理：更新菜单禁用状态
    @PostMapping(value = "/status/change")
    @Operation(description = "菜单管理：更新菜单禁用状态")
    @Parameters({
            @Parameter(name = "menuId", description = "菜单ID"),
            @Parameter(name = "status", description = "菜单禁用状态"),
    })
    public AjaxResponse updateStatus(@RequestParam Long menuId,
                                     @RequestParam Boolean status) {
        sysmenuService.updateStatus(menuId, status);
        return AjaxResponse.success("菜单禁用状态更新成功！");
    }


    //菜单管理：更新菜单隐藏状态
    @PostMapping(value = "/hidden/change")
    @Operation(description = "菜单管理：更新菜单隐藏状态")
    @Parameters({
            @Parameter(name = "menuId", description = "菜单ID"),
            @Parameter(name = "hidden", description = "菜单隐藏状态"),
    })
    public AjaxResponse updateHidden(@RequestParam Long menuId,
                                     @RequestParam Boolean hidden) {
        sysmenuService.updateHidden(menuId, hidden);
        return AjaxResponse.success("菜单隐藏状态更新成功,刷新页面即可生效！");
    }
}
